<script lang="ts" setup>
import { ref, reactive, computed } from "vue";
import { FormRules, FormInstance } from "element-plus";
import { UserApi } from "@/Api";

interface Props {
   modelValue: boolean;
   userInfo?: User.Table;
   roleList?: Role.Table[];
}

const props = withDefaults(defineProps<Props>(), {
   modelValue: false
});

const emit = defineEmits<{
   (e: "update:modelValue", value: boolean): void;
}>();

const dialogVisible = computed({
   get: () => props.modelValue,
   set: value => {
      if (!value) formRef.value?.resetFields();
      emit("update:modelValue", value);
   }
});
const userInfo = computed(() => props.userInfo);
const roleList = computed(() => props.roleList);
const loading = reactive({ add: false, form: false });
const disable = reactive({ add: false });
const formRef = ref<FormInstance>();

const form = reactive<User.Update>({
   id: "",
   account: "",
   role_id: "",
   phone: "",
   name: "",
   status: "NORMAL"
});

const formRules = reactive<FormRules>({
   phone: [{ required: true, message: "手机号码必填", trigger: "blur" }],
   role_id: [{ required: true, message: "角色必填", trigger: "blur" }]
});

async function handlerAdd(formRef?: FormInstance) {
   if (!formRef) return;
   formRef.validate(async (valid, fields) => {
      if (!valid) return;
      try {
         const { code } = await UserApi.Update(form);
         if (code === 200) {
            ElMessage.success("添加成功");
            dialogVisible.value = false;
         }
      } catch (error) {
         console.log("error", error);
      }
   });
}

/** 弹窗打开时 */
async function handlerOpen() {
   if (!userInfo.value) return;
   for (const key in form) {
      setProp(form, key as keyof User.Update, userInfo.value[key as keyof User.Table]);
   }
}

function setProp<T, K extends keyof T>(foo: T, key: K, val: T[K]) {
   foo[key] = val;
}
</script>

<template>
   <el-dialog v-model="dialogVisible" ref="dialog" title="编辑用户" width="500px" draggable append-to-body @open="handlerOpen">
      <el-form v-loading="loading.form" ref="formRef" :model="form" :rules="formRules" label-width="85">
         <el-form-item label="显示名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入用户名称" />
         </el-form-item>
         <el-form-item label="用户账户" prop="account">
            <el-input v-model="form.account" placeholder="不填则为实际号码，可使用账户+密码的方式登录" />
         </el-form-item>
         <el-form-item label="手机号码" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入手机号码" />
         </el-form-item>
         <el-form-item label="用户角色" prop="role_id">
            <el-select v-model="form.role_id" placeholder="请选择用户角色" style="width: 100%">
               <el-option v-for="item in roleList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
         </el-form-item>
      </el-form>
      <template #footer>
         <span class="dialog-footer">
            <el-button :loading="loading.add" :disabled="disable.add" type="primary" @click="handlerAdd(formRef)">
               保存
            </el-button>
         </span>
      </template>
   </el-dialog>
</template>

<style lang="scss" scoped>
.el-form {
   box-sizing: border-box;
   padding-inline: 20px;
}
.el-input {
   width: 100%;
}
</style>
